home *** CD-ROM | disk | FTP | other *** search
/ PC User 2002 April / Disc 1 / PCUSER0402D1.iso / resource / ecomm / files / ecomm205.exe / data1.cab / Template / ecomm.js < prev    next >
Encoding:
Text File  |  2001-11-26  |  10.9 KB  |  363 lines

  1. <!--
  2. // -----
  3. // item
  4. // -----
  5. function item(url, title, price, image, weight, freight, tax, option1, option2)
  6. {
  7.  
  8.   this.count = 0;
  9.   this.url = url.substring(0);
  10.   this.title = title.substring(0);
  11.   this.price = (price) ? price : 0;
  12.   this.image = image.substring(0);
  13.   this.weight = (weight) ? weight : 0;
  14.   this.freight = (freight) ? freight : 0;
  15.   this.tax = (tax) ? tax : 0;
  16.   this.option1 = option1.substring(0); 
  17.   this.option2 = option2.substring(0);
  18. }
  19.  
  20. function addItem()
  21. {
  22.   var url;
  23.   var code;
  24.   var title;
  25.   var price;
  26.   var image;
  27.   var qty;
  28.   var weight;
  29.   var freight;
  30.   var tax;
  31.   var option1;
  32.   var option2;
  33.  
  34.   for (var i = 0; i < arguments.length; i++)
  35.   {
  36.     if (i == 0) url = arguments[i];
  37.     if (i == 1) code = arguments[i];
  38.     if (i == 2) title = arguments[i];
  39.     if (i == 3) price = arguments[i];
  40.     if (i == 4) image = arguments[i];
  41.     if (i == 5) qty = arguments[i];
  42.     if (i == 6) weight = arguments[i];
  43.     if (i == 7) freight = arguments[i];
  44.     if (i == 8) tax = arguments[i];
  45.     if (i == 9) option1 = arguments[i];
  46.     if (i == 10) option2 = arguments[i];
  47.  
  48.   }  
  49.  
  50.   price=fmt_num(price);
  51.   if (list[code] == null){    
  52.     list[code] = new item(url, title, price, image, weight, freight ,tax, option1, option2);
  53.   }
  54.   list[code].count += qty;
  55.  
  56.   if (parent.shoptrolley != null)
  57.     parent.shopregister.showBasket();
  58. }
  59.  
  60. function removeItem(code)
  61. {  
  62.   for (i in list){     
  63.        if (i.indexOf(code) != -1 && list[i] != null && list[i].count > 0) {
  64.           list[i].count--;
  65.           break;
  66.        }  
  67.   }
  68.   parent.shopregister.showBasket();
  69. }
  70.  
  71. function changeQty(key)
  72. {
  73.   var qty = list[key].count;
  74.  
  75.   if ((qty = prompt("Enter new quantity", qty)) != null)
  76.     if (isNaN(qty = parseInt(qty)))
  77.       alert("Invalid number.");
  78.     else if (qty < 0)
  79.       alert("Quantity must be zero or greater.");
  80.     else {
  81.       list[key].count = qty;
  82.       showBasket();
  83.     }
  84. }
  85.  
  86.  
  87. function formatDescription(str)
  88. {
  89.   var newStr = unescape(str);
  90.  
  91.   if (newStr.length > descrLength)
  92.     newStr = newStr.substring(0, descrLength - 3) + "...";
  93.  
  94.   newStr += "</A>";
  95.  
  96.   while (newStr.length < descrLength + 4)
  97.     newStr += " ";
  98.  
  99.   return newStr;
  100. }
  101.  
  102. function formatPrice(price, width)
  103. {
  104.   var newPrice = "" + price;
  105.  
  106.   if (newPrice.lastIndexOf('.') == -1)
  107.     newPrice += '.';
  108.   else
  109.     newPrice = newPrice.substring(0, newPrice.lastIndexOf('.') + 3);
  110.  
  111.   while ((c = newPrice.lastIndexOf('.')) > newPrice.length - 3 && c > -1)
  112.     newPrice += '0';
  113.  
  114.   while (newPrice.length < width)
  115.     newPrice = ' ' + newPrice;
  116.  
  117.   return newPrice;
  118. }
  119.  
  120.  
  121. function sche(ALINK_COLOR, BGCOLOR, FRAME_COLOR, HEADING2, VLINK_COLOR, LIST_COLOR, TEXT_COLOR, LINK_COLOR,S_CELLCOLOR,S_CELLBORDER,S_BORDER,S_BORDER2,S_HEADING,S_BACKGROUND,CURRENCY)
  122. {
  123.   this.ALINK_COLOR = ALINK_COLOR;
  124.   this.BGCOLOR = BGCOLOR;
  125.   this.FRAME_COLOR = FRAME_COLOR;
  126.   this.HEADING2 = HEADING2;
  127.   this.VLINK_COLOR = VLINK_COLOR;
  128.   this.LIST_COLOR = LIST_COLOR;
  129.   this.TEXT_COLOR = TEXT_COLOR;
  130.   this.LINK_COLOR = LINK_COLOR;
  131.   this.S_CELLCOLOR = S_CELLCOLOR;
  132.   this.S_CELLBORDER= S_CELLBORDER;
  133.   this.S_BORDER= S_BORDER;
  134.   this.S_BORDER2= S_BORDER2;
  135.   this.S_HEADINGCOLOR= S_HEADING;
  136.   this.S_BACKGROUND= S_BACKGROUND;
  137.   this.CURRENCY=CURRENCY;
  138. }
  139.  
  140.  
  141. function showBasket()
  142. {
  143. // need for history 
  144. // register.html -> showBasket() -> trolley.html -> displayBasket()
  145. //
  146.   if (parent.shoptrolley != null)
  147.      if (parent.shoptrolley.location != null)
  148.          parent.shoptrolley.location = "trolley.html"; 
  149. }
  150.  
  151.  
  152. function displayBasket(){
  153.   var tablesize = 10;
  154.  
  155. // just only substitution
  156.   t_sche = parent.shopregister.t_she;
  157.  
  158.   parent.shoptrolley.document.open();
  159.   parent.shoptrolley.document.writeln('<HTML><HEAD><TITLE>Trolley</TITLE>');
  160.   parent.shoptrolley.document.writeln('<BASE TARGET="shopmain">');
  161.   parent.shoptrolley.document.writeln('</HEAD>');
  162.  
  163.  
  164.   parent.shoptrolley.document.writeln('<BODY TEXT="' + t_sche.TEXT_COLOR + 
  165.                                        '" BGCOLOR="' + t_sche.BGCOLOR + 
  166.                                        '" LINK="' + t_sche.LINK_COLOR + 
  167.                                        '" VLINK="' + t_sche.VLINK_COLOR + 
  168.                                        '" ALINK="' + t_sche.ALINK_COLOR +
  169.                                        '" BACKGROUND="' + t_sche.S_BACKGROUND
  170.                                        +'">'
  171.                                        );
  172.  
  173.  
  174.   parent.shoptrolley.document.write('<TABLE BORDER='+t_sche.S_BORDER+' CELLPADDING=0 CELLSPACING=0><TR>');
  175.   if (t_sche.HEADING2)
  176.   parent.shoptrolley.document.write('<TD>'+t_sche.HEADING2+'</td></tr><tr>');
  177.   
  178.   parent.shoptrolley.document.write('<TD BGCOLOR="'+t_sche.FRAME_COLOR+'" WIDTH=178> </TD>');
  179.   parent.shoptrolley.document.write(' </TR><TR><TD>');
  180.  
  181.   parent.shoptrolley.document.write('<TABLE WIDTH=178 BORDER=0 CELLPADDING=1 CELLSPACING=0 BGCOLOR="'+t_sche.FRAME_COLOR+'"><TR><TD>');
  182.   parent.shoptrolley.document.write('<TABLE WIDTH=100% BORDER='+t_sche.S_BORDER2+' CELLPADDING=0 CELLSPACING=0 BGCOLOR="'+t_sche.LIST_COLOR+'"><TR><TD>');
  183.   parent.shoptrolley.document.write('<TABLE WIDTH=100% BORDER='+t_sche.S_CELLBORDER+' CELLPADDING=2 CELLSPACING=4>');
  184.   parent.shoptrolley.document.write('<TR BGCOLOR="'+t_sche.LIST_COLOR+'">');
  185.   parent.shoptrolley.document.write('<TH WIDTH=20><FONT FACE="Arial,Helvetica,sans-serif" SIZE=-2 COLOR='+t_sche.S_HEADINGCOLOR+'> QTY </FONT></TH>');
  186.   parent.shoptrolley.document.write('<TH WIDTH=80><FONT FACE="Arial,Helvetica,sans-serif" SIZE=-2 COLOR='+t_sche.S_HEADINGCOLOR+'>ITEM</FONT></TH>');
  187.   parent.shoptrolley.document.write('<TH WIDTH=30><FONT FACE="Arial,Helvetica,sans-serif" SIZE=-2 COLOR='+t_sche.S_HEADINGCOLOR+'> COST </FONT></TH></TR>');
  188.  
  189.  
  190.  
  191. //
  192. // parent.shopregister.document.forms.inputForm.PrintOrder
  193. //
  194.   var doc = parent.shopregister.document.forms;
  195.   doc.inputForm.PrintOrder.value = "";
  196.   doc.inputForm.PROD.value = "15:";
  197.   doc.inputForm.OrderID.value = "";
  198.   var startIdx = location.href.indexOf("register");
  199.   var productLink = location.href.substring(0,startIdx); 
  200.  
  201.   for (i in list)
  202.     if (list[i].count > 0) {
  203.       parent.shoptrolley.document.write('<TR BGCOLOR="'+t_sche.S_CELLCOLOR+'">');
  204.       parent.shoptrolley.document.write('<TD ALIGN=CENTER"><FONT FACE="Arial,Helvetica,sans-serif" SIZE=-2>');
  205.       parent.shoptrolley.document.write('<A HREF="javascript:parent.shopregister.changeQty(\''+i+'\')">'+list[i].count+'</A></FONT></TD>');
  206.  
  207.       parent.shoptrolley.document.write('<TD><FONT FACE="Arial,Helvetica,sans-serif" SIZE=-2>');
  208.       parent.shoptrolley.document.write('<A HREF="'+list[i].url+'">'+formatDescription(unescape(list[i].title))+'</FONT></TD>');
  209.  
  210.       parent.shoptrolley.document.write('<TD NOWRAP ALIGN=RIGHT><FONT FACE="Arial,Helvetica,sans-serif" SIZE=-2>');
  211.       parent.shoptrolley.document.write(' '+t_sche.CURRENCY + formatPrice(list[i].price * list[i].count, 5) + '</FONT></TD></TR>');
  212.  
  213.       doc.inputForm.OrderID.value += i + "x" + list[i].count + ",";
  214.       doc.inputForm.PrintOrder.value += list[i].count + " x " + unescape(list[i].title +" "+list[i].option1+" "+list[i].option2+"... "+t_sche.CURRENCY+ formatPrice(list[i].price, 0)) + "<BR>";
  215.       doc.inputForm.PROD.value += list[i].count+"    "+unescape(list[i].title+"    "+i+"    "+formatPrice(list[i].price, 0))+"    "+list[i].weight+"    "+productLink+list[i].url+ "    "+t_sche.CURRENCY+"    "+list[i].freight+"    "+list[i].tax+"    0    0    0    "+list[i].option1+"    "+list[i].option2+"    <BR>"+"    " ;
  216.       tablesize--;
  217.     }
  218.  
  219.   while (tablesize-- > 0){
  220.     parent.shoptrolley.document.write('<TR BGCOLOR='+t_sche.S_CELLCOLOR+'><TD><FONT FACE="Arial,Helvetica,sans-serif" SIZE=-2> </FONT></TD>');
  221.     parent.shoptrolley.document.write('<TD><FONT FACE="Arial,Helvetica,sans-serif" SIZE=-2> </FONT></TD>');
  222.     parent.shoptrolley.document.write('<TD><FONT FACE="Arial,Helvetica,sans-serif" SIZE=-2> </FONT>');
  223.   }
  224.  
  225.   parent.shoptrolley.document.write('</TD></TR></TABLE>');
  226.   parent.shoptrolley.document.write('</TD></TR></TABLE>');
  227.   parent.shoptrolley.document.write('</TD></TR></TABLE>');
  228. //  parent.shoptrolley.document.write('</TD></TR></TABLE>');
  229.  
  230.   parent.shoptrolley.document.write('</TD></TR></TABLE>');
  231. //  parent.shoptrolley.document.write('</CENTER>');
  232.   parent.shoptrolley.document.write('</BODY></HTML>');
  233.   parent.shoptrolley.document.close();
  234.  
  235.  
  236.   doc.inputForm.Total.value = sumTotal();
  237.   doc.inputForm.Weight.value = sumWeight();
  238.   doc.inputForm.AddFreight.value = sumFreight();
  239.   doc.inputForm.TaxVal.value = sumTax();
  240. }
  241.  
  242. function sumTotal()
  243. {
  244.   var total = 0.0;
  245.   for (i in list) total += (list[i].price * list[i].count);
  246.   return formatPrice(total, 7);
  247. }
  248.  
  249. function sumWeight()
  250. {
  251.   var total = 0.0;
  252.   for (i in list) total += (list[i].weight * list[i].count);
  253.   if (total > 0 && total < 0.01)
  254.      total = 0.01;
  255.   return total;
  256. }
  257.  
  258. function sumFreight()
  259. {
  260.   var total = 0.0;
  261.   for (i in list) total += (list[i].freight * list[i].count);
  262.   return total;
  263. }
  264.  
  265. function sumTax()
  266. {
  267.   var total = 0.0;
  268.   for (i in list) total += (list[i].price * list[i].count * list[i].tax/100);
  269.   return total;
  270. }
  271.  
  272. function inTrolley()
  273. {
  274.    var q = 0;
  275.        
  276.    for (i in list){
  277.        if (list[i] != null && list[i].count > 0)
  278.           q += list[i].count;
  279.    } 
  280.    if (q > 0)
  281.       return true; 
  282.    else
  283.       return false; 
  284. }
  285.  
  286. //--
  287. // is str number?
  288. //--
  289.  function isNum(str) {
  290.    if(!str) return false;
  291.    for(var i=0; i<str.length; i++){
  292.      var ch=str.charAt(i);
  293.      if ("0123456789 .,".indexOf(ch) ==-1) return false;
  294.    }
  295.    return true;
  296.  } 
  297.  
  298. //--
  299. // change format of price:
  300. // 1,2.34,567.89  --> 1234567.89
  301. // 1,2.34,567,89  --> 1234567.89
  302. // 1,2.3 4,567,89 --> 1234567.89
  303. // 1,2.3a 4,567,89--> 0
  304. // 12.343         --> 12.343
  305. // 12,34          --> 12.34
  306. // 123            --> 123
  307.  function fmt_num(str) {
  308. var value=0.0;
  309. var x= str.lastIndexOf(".");
  310. var y= str.lastIndexOf(",");
  311.  
  312.  if ( !isNum(str) )
  313.    return(value);
  314.  
  315.  
  316.  if ( x == -1  && y == -1 ){
  317.    value = parseInt(str);
  318.    return(value);
  319.  }
  320.  
  321.  if ( x < y )
  322.     x=y;
  323.  
  324.  txt1=str.substring(0,x);
  325.  txt1=replace(txt1,',',''); 
  326.  txt1=replace(txt1,'.',''); 
  327.  txt1=replace(txt1,' ',''); 
  328.    
  329.  txt=txt1+'.'+str.substring(x+1);
  330.  value = (parseInt(txt1) + parseFloat('.'+str.substring(x+1)));
  331.  
  332. return(value);
  333. }
  334.  
  335. //-- replace 
  336. function replace(string,pattern,insert) {
  337.  var strLength = string.length, txtLength = pattern.length;
  338.  
  339.  if ((strLength == 0) || (txtLength == 0)) 
  340.     return string;
  341.  
  342.  var i = string.indexOf(pattern);
  343.  if ((!i) && (pattern != string.substring(0,txtLength))) 
  344.      return string;
  345.  if (i == -1) 
  346.     return string;
  347.  
  348.  var newstr = string.substring(0,i) + insert;
  349.  
  350.  if (i+txtLength < strLength)
  351.     newstr += replace(string.substring(i+txtLength,strLength),pattern,insert);
  352.  
  353. return newstr;
  354. }
  355.  
  356.  
  357. // --- MAIN ---
  358. var descrLength = 16;
  359. var t_she='1';
  360. list = new Array();
  361.  
  362. // -->
  363.